/*
Copyright (C) 2009-2010 wxLauncher Team

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

#include <wx/wx.h>
#include <wx/dir.h>
#include <wx/filename.h>
#include <wx/wfstream.h>
#include "generated/configure_launcher.h"
#include "apis/PlatformProfileManager.h"
#include "controls/LightingPresets.h"
#include "global/ids.h"


// Determine if `path` is writable, creating any missing parent directories if
// possible.
//
// If `path` is an existing file or directory, return true if that file is
// writable. If `path` is a file which does not exist, create all parent
// directories if possible, and return true if the file can be created. If
// `path` is a directory which does not exist, try creating it and any missing
// parent directories, and return `true` if successful.
bool CreateWritablePath(wxFileName &path) {
	wxLogInfo(_T("CreateWritablePath(%s)"), path.GetFullPath().c_str());

	if(path.HasName()) {
		if(path.FileExists()) {
			return path.IsFileWritable();
		} else {
			// The given file does not exist; find its closest existing
			// ancestor and create all needed directories in between.
			wxFileName parent = wxFileName::DirName(path.GetPath());

			return CreateWritablePath(parent);
		}
	}

	if(path.DirExists()) {
		return path.IsDirWritable();
	} else {
		wxFileName parent = wxFileName::DirName(path.GetPath());
		parent.RemoveLastDir();

		if(CreateWritablePath(parent)) {
			if(path.Mkdir()) {
				wxLogDebug(_T("Created missing folder %s"),
						path.GetFullPath().c_str());
				return true;
			} else {
				wxLogDebug(_T("Couldn't create missing folder %s"),
						path.GetFullPath().c_str());
			}
		}
	}

	wxLogInfo(_T("Fell through the bottom of the world."));
	return false;
}

ProMan::RegistryCodes PushCmdlineFSO(wxFileConfig *cfg) {
	wxString modLine, flagLine, tcPath;
	cfg->Read(PRO_CFG_TC_CURRENT_MODLINE, &modLine);
	cfg->Read(PRO_CFG_TC_CURRENT_FLAG_LINE, &flagLine);
	cfg->Read(PRO_CFG_TC_ROOT_FOLDER, &tcPath);

	wxString presetName;
	wxString lightingPresetString;
	if (cfg->Read(PRO_CFG_LIGHTING_PRESET, &presetName)) {
		lightingPresetString = LightingPresets::PresetNameToPresetString(presetName);
	}

	wxFileName cmdLineFileName(GetPlatformDefaultConfigFilePath());
	cmdLineFileName.SetFullName(_T("cmdline_fso.cfg"));

	if(!CreateWritablePath(cmdLineFileName)) {
		wxLogError(_T("Couldn't write command line arguments to home directory"));
		return ProMan::UnknownError;
	}

	wxFFileOutputStream outStream(cmdLineFileName.GetFullPath(), _T("w+b"));
	if ( !outStream.IsOk() ) {
		return ProMan::UnknownError;
	}
	if ( !modLine.IsEmpty()) {
		outStream.Write("-mod ", 5);
		outStream.Write(modLine.char_str(), modLine.size());
	}
	if ( !flagLine.IsEmpty() ) {
		outStream.Write(" ", 1);
		outStream.Write(flagLine.char_str(), flagLine.size());
	}
	if ( !lightingPresetString.IsEmpty()) {
		outStream.Write(" ", 1);
		outStream.Write(lightingPresetString.char_str(), lightingPresetString.size());
	}
	if ( !outStream.Close() ) {
		return ProMan::UnknownError;
	}

	return ProMan::NoError;
}
